package com.lyn.module_custom_utils.time;


import android.content.Context;
import android.text.TextUtils;

import com.lyn.module_custom_utils.log.LogUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author:longyongning
 * @date:2021/10/2
 * @description:日期工具类
 */
public class TimeUtil {
    private static class TimeUtilsClassType {
        private static TimeUtil instance = new TimeUtil();
    }

    public static TimeUtil getInstance() {
        return TimeUtilsClassType.instance;
    }

    /**
     * 获取系统时间
     */
    public String getCurrentDate() {
        return new SimpleDateFormat("yyyy-MM-dd HH.mm.ss").format(new Date());
    }

    public int[] getDateToIntArray(long time) {
        Date date = new Date();
        date.setTime(time);
        String arry = new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss").format(date);
        String[] arrayTimes = arry.split(":");
        int[] arrayTime = new int[6];

        for (int i = 0; i < arrayTimes.length; i++) {
            arrayTime[i] = Integer.parseInt(arrayTimes[i]);
        }
        return arrayTime;
    }

    public String getDateToString(long time) {
        Date date = new Date();
        date.setTime(time);
        return new SimpleDateFormat("yyyy-MM-dd HH.mm.ss").format(date);
    }

    public String getDateToString2(long time) {
        Date date = new Date();
        date.setTime(time);
        return new SimpleDateFormat("HH.mm.ss").format(date);
    }

    public String getDateToString3(long time) {
        Date date = new Date();
        date.setTime(time);
        return new SimpleDateFormat("yyyy年MM月dd日").format(date);
    }

    public String getDateToString4(long time) {
        Date date = new Date();
        date.setTime(time);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    public String getDateToString5(long time) {
        Date date = new Date();
        date.setTime(time);
        return new SimpleDateFormat("HH:mm").format(date);
    }

    /**
     * 将字符串转为时间戳
     */
    public long getStringToDate(String time) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH.mm.ss").parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }

    /**
     * 将字符串转为时间戳
     */
    public long getStringToDate2(String time) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }

    /**
     * 将字符串转为时间戳
     */
    public long getStringToDate3(String time) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }

    /**
     * 将字符串转为日期时间戳
     */
    public long getStringToDateDay(String time) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }


    /**
     * 获取本月第一天日期时间戳
     *
     * @return
     */
    public long getSomeday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0); //获取当前月第一天
        c.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
        c.set(Calendar.MINUTE, 0); //将分钟至0
        c.set(Calendar.SECOND, 0); //将秒至0
        c.set(Calendar.MILLISECOND, 0); //将毫秒至0
        return c.getTimeInMillis();
    }


    /**
     * 获取本月最后一天日期时间戳
     *
     * @return
     */
    public long getSomedayEnd() {
        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.DAY_OF_MONTH, c2.getActualMaximum(Calendar.DAY_OF_MONTH)); //获取当前月最后一天
        c2.set(Calendar.HOUR_OF_DAY, 23); //将小时至23
        c2.set(Calendar.MINUTE, 59); //将分钟至59
        c2.set(Calendar.SECOND, 59); //将秒至59
        c2.set(Calendar.MILLISECOND, 999); //将毫秒至999
        return c2.getTimeInMillis();
    }

    /**
     * 获取本月某一天日期开始时间戳
     *
     * @return
     */
    public long getSomeday(int month, int day, int hour, int minute, int s, int ms) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH,month<=0?0:month-1);//月份
        c.set(Calendar.DAY_OF_MONTH, day); //设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR_OF_DAY, hour); //将小时至0
        c.set(Calendar.MINUTE, minute); //将分钟至0
        c.set(Calendar.SECOND, s); //将秒至0
        c.set(Calendar.MILLISECOND, ms); //将毫秒至0
        LogUtil.i("month:"+month);
        LogUtil.i("时间戳:"+c.getTimeInMillis());
        return c.getTimeInMillis();
    }



    /**
     * 获取每天的开始时间 00:00:00:00
     *
     * @return
     */
    public long getTodayStartTime() {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(new Date(System.currentTimeMillis()));
        dateStart.set(Calendar.HOUR_OF_DAY, 0);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        return dateStart.getTime().getTime();
    }

    /**
     * 获取每天的结束时间 23:59:59:999
     *
     * @return
     */
    public long getTodayEndTime() {
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(new Date(System.currentTimeMillis()));
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        return dateEnd.getTime().getTime();
    }

    //获取当前月份天数
    public int getDaysOfMonth(long time) {
        try {
            Calendar calendar = Calendar.getInstance();
            Date date = new Date();
            date.setTime(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setTime(sdf.parse(sdf.format(date)));
            return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //获取当前日
    public int getTheDay(long time) {
        try {
            Date date = new Date();
            date.setTime(time);
            SimpleDateFormat sdf = new SimpleDateFormat("dd");
            return Integer.parseInt(sdf.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //获取当前月份
    public int getTheMonth(String time) {
        try {
            Date date = new Date();
            date.setTime(getStringToDateDay(time));
            SimpleDateFormat sdf = new SimpleDateFormat("MM");
            return Integer.parseInt(sdf.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取星期
     *
     * @param context
     * @return
     */
    public final String getWeek(Context context,String ... week_array) {
        Date date = new Date();
        date.setTime(System.currentTimeMillis());
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return week_array[w];
    }

    //获取本周星期索引
    public final int getWeekIndex(Context context) {
        Date date = new Date();
        date.setTime(System.currentTimeMillis());
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return w;
    }

    //获取本周的开始时间
    public long getBeginDayOfWeek() {
        Date date = new Date(getTodayStartTime());
        if (date == null) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return cal.getTime().getTime();
    }

    public long getBeginDayOfWeek2() {
        Date date = new Date(getTodayEndTime());
        if (date == null) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return cal.getTime().getTime();
    }

    //获取本周的结束时间
    public long getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(getBeginDayOfWeek2()));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return weekEndSta.getTime();
    }

    /**
     * 验证当前时间是否在时间段范围
     *
     * @param start
     * @param stop
     * @return
     */
    public boolean isPeriod(long start, long stop) {
        long current = System.currentTimeMillis();
        if (current >= start && current <= stop) {
            return true;
        } else {
            return false;
        }
    }



    //验证在当前时间段
    public boolean VerPeriod(String startTime, String endTime) {
        try {
            long start = new SimpleDateFormat("HH:mm").parse(startTime).getTime();
            long end = new SimpleDateFormat("HH:mm").parse(endTime).getTime();
            Date date = new Date();
            date.setTime(System.currentTimeMillis());
            long current = new SimpleDateFormat("HH:mm").parse(new SimpleDateFormat("HH:mm").format(date)).getTime();
            LogUtil.d("start:" + start + "end:" + end + "current:" + current);
            if (current > start && current < end) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //验证在当前时间段time[0]当前要比对的时间time[1]开始时间time[2]结束时间time[3]第二段时间开始时间
    public int VerPeriod(long... time) {
        try {
            if (time[0] >= time[1] && time[0] <= time[2]) {
                //正常
                return 1;
            } else {
                if (time[0] > time[2]&&time[0]<time[3]) {
                    //迟到
                    return 2;
                }else {
                    //缺卡
                    return 3;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean checkTimeFormat(String strww, String strtt) {
        try {
            if (!match(strww)) {
                return false;
            }

            String[] strtimes = strtt.split(",");
            int count = strtimes.length;
            if (count % 2 != 0 && count > 6) {
                return false;
            }
            long[] ltimes = new long[count];
            for (int i = 0; i < count; i++) {
                String strt = strtimes[i];
                long ltime = new SimpleDateFormat("HH:mm:ss").parse(strt).getTime();
                ltimes[i] = ltime;
            }
            if (count > 1) {
                if (ltimes[1] < ltimes[0]) {
                    return false;
                }
            }
            if (count > 3) {
                if (ltimes[3] < ltimes[2]) {
                    return false;
                }
            }
            if (count > 5) {
                if (ltimes[5] < ltimes[4]) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public  boolean match(String sn) {
        if ((TextUtils.isEmpty(sn))) {
            return false;
        }
        Pattern p = Pattern.compile("[1-7]*");
        Matcher m = p.matcher(sn);
        if (!m.matches()) {
            return false;
        }
        return true;
    }

    //获取本周星期索引
    public final int getWeekofDay2(Context context) {
        Date date = new Date();
        date.setTime(System.currentTimeMillis());
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            LogUtil.i("当前没有勾选星期");
            w = 0;
        }
        LogUtil.i("当前没有勾选星期：" + w);
        return w;
    }
}

